﻿using MetroDataApi.Models.ModelDefination;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class ProjectStatusHelper
    {
        public static List<statusTimeDate> GetMainStatusGroup(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var list = from t in db.PrjStatus
                           where t.PrjId == prjId && t.Status != ""
                           orderby t.Date.Value
                           select new
                           {
                               time = t.Date.Value,
                               status = t.Status
                           };

                var sList = new List<statusTimeDate>();
                var tempStatus = "";
                foreach (var item in list)
                {
                    if (tempStatus != item.status)
                    {
                        var s = new statusTimeDate()
                        {
                            status = item.status,
                            time = item.time.ToString("yyyy-MM-dd")
                        };
                        sList.Add(s);
                        tempStatus = item.status;
                    }
                }
                return sList;
            }

        }

        private static List<projectStatus> GenerateStatusGroup(int prjId, bool partitial = true)
        {
            var list = new List<projectStatus>();
            using (var db = new MetroDataDataContext())
            {
                var status = from p in db.PrjStatus where p.PrjId == prjId orderby p.Date.Value ascending select p;
                var tempTitle = "";
                projectStatus prjSt = null;
                foreach (var s in status)
                {
                    if (s.Status != tempTitle)
                    {
                        tempTitle = s.Status;
                        if (prjSt != null) list.Add(prjSt);

                        prjSt = new projectStatus()
                        {
                            title = s.Status,
                            endTime = s.Date.Value.ToString("yyyy-MM-dd")
                        };

                        if (list.Count == 0)
                        {
                            prjSt.startTime = prjSt.endTime;
                        }
                        else
                        {
                            prjSt.startTime = list.Last().endTime;
                        }


                        var sub = new projectStatusSub()
                        {
                            startTime = prjSt.startTime,
                            endTime = prjSt.endTime,
                            title = prjSt.title,
                            detail = s.Description,
                            imagesUrl = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date.Value.Date == s.Date.Value.Date)
                                    .Select(p => p.Url).ToList()

                        };

                        prjSt.subItems.Add(sub);

                    }
                    else
                    {
                        var sub = new projectStatusSub()
                        {
                            startTime = prjSt.subItems.Last().endTime,
                            endTime = s.Date.Value.ToString("yyyy-MM-dd"),
                            title = s.Status,
                            detail = s.Description,
                            imagesUrl = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date.Value.Date == s.Date.Value.Date)
                                                   .Select(p => p.Url).ToList()

                        };


                        prjSt.subItems.Add(sub);
                    }
                }
                if (prjSt != null) list.Add(prjSt);
                return list;
            }
        }


        private static List<projectStatus> GenerateStatusGroup222(int prjId, bool partitial = true)
        {
            var list = new List<projectStatus>();

            using (var db = new MetroDataDataContext())
            {
                var status = (from p in db.PrjStatus
                              where p.PrjId == prjId
                              orderby p.Date.Value ascending
                              group p by p.Status into g
                              orderby g.First().Date.Value
                              select g).ToList();
                foreach (var i in status)
                {
                    var item = new projectStatus()
                    {
                        title = i.Key,
                        endTime = i.Last().Date.Value.ToString("yyyy-MM-dd")
                    };
                    if (list.Count == 0)
                    {
                        item.startTime = item.endTime;
                    }
                    else
                    {
                        item.startTime = list.Last().endTime;
                    }
                    var temp = i.OrderBy(p => p.Date.Value);
                    foreach (var sub in temp)
                    {
                        var subItem = new projectStatusSub()
                        {
                            endTime = sub.Date.Value.ToString("yyyy-MM-dd"),
                            detail = sub.Description,
                            title = sub.Status
                        };
                        if (partitial == true)
                        {
                            subItem.imagesUrl = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date.Value.Date == sub.Date.Value.Date)
                                                   .Select(p => p.Url).ToList();
                        }
                        else
                        {
                            subItem.imagesUrl = null;
                        }

                        if (item.subItems.Count == 0)
                        {
                            subItem.startTime = item.startTime;
                        }
                        else
                        {
                            subItem.startTime = item.subItems.Last().endTime;
                        }
                        item.subItems.Add(subItem);
                    }

                    list.Add(item);
                }
            }
            return list;
        }

        public static List<projectStatus> GetPartitialStatusById(int prjId)
        {
            var list = ProjectStatusHelper.GenerateStatusGroup(prjId);
            var st = list.Last();
            if (st.startTime == st.endTime)
            {

            }
            else
            {
                st.subItems.First().changes = Recent2Changes.AllChanges(prjId, st.startTime, st.endTime);
            }
            return list;
        }

        public static List<projectStatus> GetStatusById(int prjId)
        {
            var list = ProjectStatusHelper.GenerateStatusGroup(prjId, false);
            ProjectStatusHelper.FillInChanges(prjId, list);
            return list;
        }


        private static void FillInChanges(int prjId, List<projectStatus> list)
        {
            foreach (var prjStatus in list)
            {
                if (prjStatus.startTime == prjStatus.endTime)
                {
                    //prjStatus.changes = new List<recentChanges>();
                }
                else
                {
                    prjStatus.subItems.First().changes =
                        Recent2Changes.AllChanges(prjId, prjStatus.startTime, prjStatus.endTime);
                }
            }
        }



        public static projectStatusSub GetLastestStatus(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var k = (from p in db.PrjStatus
                         where p.PrjId == prjId
                         orderby p.Date.Value
                             descending
                         select p).FirstOrDefault();

                if (k == null)
                {
                    return null;
                }
                else
                {
                    var sub = new projectStatusSub()
                    {
                        detail = k.Description,
                        title = k.Status,
                        imagesUrl = db.PrjStatusImage.Where(p => p.PrjId == prjId && p.Date.Value.Date == k.Date.Value.Date)
                           .Select(p => p.Url).ToList(),
                        endTime = k.Date.Value.ToString("yyyy-MM-dd")
                    };
                    return sub;
                }
            }

        }




    }
}